package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import domain.TagType;

public class TagTypeDAO {

	private static EmbeddedDatabaseType edt = EmbeddedDatabaseType.DERBY; 
	private static EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
	private static EmbeddedDatabase db = (EmbeddedDatabase) builder.setType(edt).addScript("schema.sql").build();
 	
	private static final String SELECT_ALL_TAG_TYPES_QUERY = "SELECT id,tag_type FROM penguin.tag_type";
	
	public List<TagType> selectAllTagTypes(){
		JdbcTemplate template = new JdbcTemplate(db);
        List<TagType> result = template.query(SELECT_ALL_TAG_TYPES_QUERY,new TagTypeRowMapper());
        return result;
	}
	
	class TagTypeRowMapper implements RowMapper<TagType> {
		public TagType mapRow(ResultSet rs, int rowNum) throws SQLException {
			TagType myTagType = new TagType();
			myTagType.setId(rs.getInt("id"));
			myTagType.setTag_type(rs.getString("tag_type"));
			return myTagType;
		}
	}
	
}
